package com.funsport.controller.mobile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.PageResult;
import com.funsport.common.Result;
import com.funsport.entity.Venue;
import com.funsport.service.IVenueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 移动端场地Controller
 *
 * @author Fun-Sport Team
 */
@Slf4j
@Tag(name = "移动端场地", description = "移动端场地相关接口")
@RestController
@RequestMapping("/mobile/venue")
@RequiredArgsConstructor
public class MobileVenueController {

    private final IVenueService venueService;

    @Operation(summary = "搜索场地")
    @GetMapping("/search")
    public Result<List<Venue>> searchVenues(
            @RequestParam(required = false) String city,
            @RequestParam(required = false) Long sportTypeId) {
        List<Venue> venues = venueService.searchVenues(city, sportTypeId);
        return Result.success(venues);
    }

    @Operation(summary = "获取场地列表")
    @GetMapping("/list")
    public Result<PageResult<Venue>> getVenueList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String city) {
        
        Page<Venue> pageParam = new Page<>(page, pageSize);
        // 可以添加城市筛选
        Page<Venue> result = venueService.page(pageParam);
        
        return Result.success(PageResult.build(
                result.getRecords(), result.getTotal(), page, pageSize));
    }

    @Operation(summary = "获取场地详情")
    @GetMapping("/{id}")
    public Result<Venue> getVenueDetail(@PathVariable Long id) {
        Venue venue = venueService.getById(id);
        return Result.success(venue);
    }

    @Operation(summary = "检查场地可用性")
    @GetMapping("/{id}/availability")
    public Result<Boolean> checkAvailability(
            @PathVariable Long id,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        boolean available = venueService.isVenueAvailable(id, startTime, endTime);
        return Result.success(available);
    }
}

